package leetCode.offer48;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution2 implements Solution {


    @Override
    public int lengthOfLongestSubstring(String s) {
        if ("".equals(s)) return 0;
        if(s.length()==1) return 1;

        char[] chars = s.toCharArray();
        String result = chars[0] + "";
        String tempStr = "";
        int max = 0;
        for (int i = 1; i < s.length(); i++) {
            if (!result.contains(chars[i] + "")){
                result += chars[i];
            }else {
                tempStr = result;
                if (tempStr.contains(chars[i] + "")){
                    result = tempStr.substring(tempStr.indexOf(chars[i]) + 1) + chars[i] + "";
                }else {
                    result = chars[i] + "";
                }
            }
            if(max< Math.max(result.length(), tempStr.length())){
                max = Math.max(result.length(), tempStr.length());
            }
        }

        return max;
    }

    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        System.out.println(solution2.lengthOfLongestSubstring("abcabcbb"));
    }
}
